﻿package com.cilico.utils;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

import com.cilico.R;
import com.cilico.view.BaseView;

/**
 * 
 * @author lv.xiangan
 *
 */
public class ConstantRepository {

	private static Bitmap uncheckImg_; // CheckBox未选
	private static Bitmap checkImg_; // CheckBox已选
	public static Bitmap unRadioImg_; // RadioBox图片
	public static Bitmap radioImg_;

	public static Bitmap getCheckBoxIcon(boolean isChecked, BaseView bv) {
		createCheckBoxIcons(bv);
		if (isChecked) {
			return checkImg_;
		} else {
			return uncheckImg_;
		}
	}

	/**
	 * 创建CheckBox图片
	 * @param bv
	 */
	private static void createCheckBoxIcons(BaseView bv) {
		Bitmap checkAllImg = null;
		try {
			checkAllImg = ConstantRepository.decodeBitmap(bv.getResources(), R.drawable.ckbox_all);
		} catch (Exception ex) {
			Utils.printException(ex);
		}
		if (checkAllImg != null) {
			int w = checkAllImg.getWidth() / 2;
			int h = checkAllImg.getHeight();
			uncheckImg_ = Bitmap.createBitmap(checkAllImg, 0, 0, w, h);
			checkImg_ = Bitmap.createBitmap(checkAllImg, w, 0, w, h);
			if (!checkAllImg.isRecycled()) {
				checkAllImg.recycle();
			}
			checkAllImg = null;
		}
	}

	public static Bitmap getRadioBoxIcon(boolean isChecked, BaseView bv) {
		if (unRadioImg_ == null || radioImg_ == null) {
			createRadioBoxIcons(bv);
		}
		if (isChecked) {
			return radioImg_;
		} else {
			return unRadioImg_;
		}
	}

	/**
	 * 创建RadioBox图片
	 * @param bv
	 */
	private static void createRadioBoxIcons(BaseView bv) {
		Bitmap checkAllImg = null;
		try {
			checkAllImg = ConstantRepository.decodeBitmap(bv.getResources(), R.drawable.robox_all);
		} catch (Exception ex) {
			Utils.printException(ex);
		}
		if (checkAllImg != null) {
			int w = checkAllImg.getWidth() / 2;
			int h = checkAllImg.getHeight();
			unRadioImg_ = Bitmap.createBitmap(checkAllImg, 0, 0, w, h);
			radioImg_ = Bitmap.createBitmap(checkAllImg, w, 0, w, h);
			if (!checkAllImg.isRecycled()) {
				checkAllImg.recycle();
			}
			checkAllImg = null;
		}
	}

	// 等待对话框提示语
	public final static int LOGIN = 0;
	public final static int LOADING_DATA = 1; // "正在下载数据";
	public final static int SENDING_DATA = 2; // "正在发送数据";
	public final static int HELPING = 3;
	final static int SET_LOCATION = 4;
	final static int LOADING = 5;
	final static int SENDING = 6;
	final static int SAVING = 7;
	final static int DELETING = 8;
	final static int CHANGING_CHANNEL = 9;
	final static int SAVING_DATA = 10;
	public final static int INIT_MAINVIEW = 11;

	public static String getWaitText(int index) {
		String text = null;
		switch (index) {
		case LOGIN:
			text = "正在登录";
			break;
		case LOADING_DATA:
			text = "获取中，请稍候...";
			break;
		case SENDING_DATA:
			text = "获取中，请稍候...";
			break;
		case HELPING:
			text = "正在载入帮助";
			break;
		case SET_LOCATION:
			text = "设定位置";
			break;
		case LOADING:
			text = "正在载入";
			break;
		case SENDING:
			text = "正在发送";
			break;
		case SAVING:
			text = "正在保存";
			break;
		case DELETING:
			text = "正在删除";
			break;
		case CHANGING_CHANNEL:
			text = "正在切换频道";
			break;
		case SAVING_DATA:
			text = "正在保存数据";
			break;
		case INIT_MAINVIEW:
			text = "初始化界面";
			break;
		default:
			text = null;
			break;
		}
		return text;
	}

	public static Bitmap decodeBitmap(Resources rs, int resId) {
		Bitmap bitmap = null;
		BitmapFactory.Options opts = new BitmapFactory.Options();
		try {
			BitmapFactory.Options.class.getField("inPurgeable").set(opts, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			bitmap = Utils.getScaledBitmap(rs, resId, opts);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitmap;
	}

}
